//
//  DeviceTools.m
//  BZBN
//
//  Created by chen on 2017/8/16.
//  Copyright © 2017年 KuoShuChing. All rights reserved.
//

#import "DeviceTools.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

#import <AVFoundation/AVFoundation.h>
#import <Accelerate/Accelerate.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
#import<AssetsLibrary/ALAssetsLibrary.h>

@implementation DeviceTools
+ (BOOL)isAllowedToUsePhotoAlbum
{
    BOOL isAllowed = NO;
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    switch (author) {
        case ALAuthorizationStatusNotDetermined://第一次询问
            isAllowed = YES;
            break;
        case ALAuthorizationStatusRestricted:
            break;
        case ALAuthorizationStatusDenied:
            break;
        case ALAuthorizationStatusAuthorized:
            isAllowed = YES;
            break;
        default:
            break;
    }
    return isAllowed;
}

+ (BOOL)isAllowedToUseCamera
{
    BOOL isAllowed = NO;
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined://第一次询问
        {
            isAllowed = YES;
            [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
                if (granted) {
                    NSLog(@"用户允许");
                }
                else
                {
                    NSLog(@"用户拒绝");
                }
            }];
        }
            break;
        case AVAuthorizationStatusRestricted:
            break;
        case AVAuthorizationStatusDenied:
            break;
        case AVAuthorizationStatusAuthorized:
            isAllowed = YES;
            break;
        default:
            break;
    }
    return isAllowed;
}

+ (BOOL)isAllowedToUseEbook {
    BOOL allow = NO;
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        allow = YES;
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        allow = YES;
    }
    else
    {
        allow = NO;
    }
    return allow;
}

//判断设备是否为静音
+ (BOOL)deviceIsSilenced {
#if TARGET_IPHONE_SIMULATOR
    return NO;
#endif
    //    CFStringRef state;
    //    UInt32 propertySize = sizeof(CFStringRef);
    //    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    //
    //    if(CFStringGetLength(state) == 0) {
    //        return YES;
    //    }
    //    else {
    //        return NO;
    //    }
    
    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL,
                           NULL,
                           NULL,
                           NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute,
                            &propertySize, &state);
    if(CFStringGetLength(state) >0 )
        return NO;
    else
        return YES;
}

+ (BOOL)isAllowedToUseBackgroundFeach {
    if ([[UIApplication sharedApplication] backgroundRefreshStatus] != UIBackgroundRefreshStatusAvailable) {
        return NO;
    }
    return YES;
}

/*
+ (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if (kSystemVersion >= 7.0)
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                bCanRecord = granted;
            }];
        }
    }
    return bCanRecord;
}

+ (BOOL)isAllowedNotification {
    //iOS8 check if user allow notification
    
    if (kSystemVersion >= 8.0000 ) {// system is iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        
        if (UIUserNotificationTypeNone != setting.types) {
            
            return YES;
            
        }
        
    } else {//iOS7
        
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        
        if(UIRemoteNotificationTypeNone != type)
            
            return YES;
    }
    
    
    return NO;
}

+ (BOOL)isAllowedToUseLocation {
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {

        //定位功能可用
        return YES;
    }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {

        //定位不能用
        return NO;
    }
    return NO;
}

+ (void)goAppSettingVC:(NSString *)name {
    if (kSystemVersion > 8.0000) {
        [UIAlertView showAlertViewWithTitle:[NSString stringWithFormat:@"用户拒绝访问%@",name] message:@"请到『设置->隐私』授权访问" cancelButtonTitle:@"取消" otherButtonTitle:@[@"设置"] onDismiss:^(int buttonIndex) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                [[UIApplication sharedApplication] openURL:url];
            }
        } onCancel:^{

        }];
    } else {
        [UIAlertView showAlertViewWithTitle:[NSString stringWithFormat:@"用户拒绝访问%@",name] message:@"请到『设置->隐私』授权访问" cancelButtonTitle:@"知道了" otherButtonTitle:nil onDismiss:^(int buttonIndex) {
        } onCancel:^{

        }];
    }
}
*/
@end
